package manage.controller;

import manage.dao.UserMapper;
import manage.model.Attendance;
import manage.model.Record;
import manage.model.User;
import manage.service.impl.RecordServiceImpl;
import manage.util.LogUtil;
import manage.util.Msg;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class RecordController {
    @Autowired
    private RecordServiceImpl recordService;

    @Autowired
    private UserMapper userMapper;

    private User user= null;

    //返回record的list
    @RequestMapping("/record")
    @ResponseBody
    public Msg Recorddata(HttpServletRequest request, Authentication authentication){
        String page = request.getParameter("currentPage");
        String limit = request.getParameter("pageSize");
        UserDetails userDetails =(UserDetails) authentication.getPrincipal();
        user = userMapper.selectByPrimaryKey(userDetails.getUsername());
        return Msg.success().add("data",recordService.selectAll(page,limit));
    }

    //添加record
    @ResponseBody
    @RequestMapping(value = "/addRecord",method = RequestMethod.POST)
    public Msg addRecord(Record record){
        record.setOperatorId(user.getAccount());
        String s= recordService.insert(record);
        LogUtil.writeFileLog(user.getAccount(),"添加档案："+record.toString());
        return Msg.attendanceAdd(s);
    }

    //修改record
    @ResponseBody
    @RequestMapping(value = "/addRecord",method = RequestMethod.PUT)
    public Msg updateRecord(Record record){
        record.setOperatorId(user.getAccount());
        String s= recordService.update(record);
        LogUtil.writeFileLog(user.getAccount(),"添加档案-改："+record.toString());
        return Msg.attendanceAdd(s);
    }
    //删除record
    @ResponseBody
    @RequestMapping("/delRecord")
    public Msg delRecord(@RequestBody Integer id){
        String s = recordService.del(id);
        LogUtil.writeFileLog(user.getAccount(),"删除档案："+id.toString());
        return Msg.attendanceAdd(s);
    }

    //根据主键查record 和修改配合使用
    @ResponseBody
    @RequestMapping("/recordSelect")
    public Msg selRecord(@RequestBody Integer id){
        Record record = recordService.select(id);
        LogUtil.writeFileLog(user.getAccount(),"添加档案-查："+record.toString());
        return Msg.success().add("record",record);
    }
}
